package com.ego.order.po;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.math.BigDecimal;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ego.common.constant.OrderConstant;
import com.ego.common.to.order.OrderTo;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.experimental.Accessors;

import static com.ego.common.constant.OrderConstant.StatusEnum.*;

/**
 * @program: ego
 * @description 订单
 * @author KamTang
 * @date 2022-11-13 11:25:07
 */
@Data
@TableName("oms_order")
@Accessors(chain = true)
public class Order implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * id
	 */
	@TableId
	private Long id;
	/**
	 * member_id
	 */
	private Long memberId;
	/**
	 * 订单号
	 */
	private String orderSn;
	/**
	 * create_time
	 */
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	private Date createTime;
	/**
	 * 用户名
	 */
	private String memberUsername;
	/**
	 * 订单总额
	 */
	private BigDecimal totalAmount;
	/**
	 * 应付总额
	 */
	private BigDecimal payAmount;
	/**
	 * 运费金额
	 */
	private BigDecimal freightAmount;
	/**
	 * 积分抵扣金额
	 */
	private BigDecimal integrationAmount;
	/**
	 * 支付方式【1->支付宝；2->微信；3->银联； 4->货到付款；】
	 */
	private Integer payType;
	/**
	 * 订单来源[0->PC订单；1->app订单]
	 */
	private Integer sourceType;
	/**
	 * 订单状态【0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单】
	 */
	private Integer status;
	/**
	 * 物流公司(配送方式)
	 */
	private String deliveryCompany;
	/**
	 * 物流单号
	 */
	private String deliverySn;
	/**
	 * 自动确认时间（天）
	 */
	private Integer autoConfirmDay;
	/**
	 * 收货人姓名
	 */
	private String receiverName;
	/**
	 * 收货人电话
	 */
	private String receiverPhone;
	/**
	 * 收货人邮编
	 */
	private String receiverPostCode;
	/**
	 * 省份/直辖市
	 */
	private String receiverProvince;
	/**
	 * 城市
	 */
	private String receiverCity;
	/**
	 * 区
	 */
	private String receiverRegion;
	/**
	 * 详细地址
	 */
	private String receiverDetailAddress;
	/**
	 * 确认收货状态[0->未确认；1->已确认]
	 */
	private Integer confirmStatus;
	/**
	 * 删除状态【0->未删除；1->已删除】
	 */
	private Integer deleteStatus;
	/**
	 * 支付时间
	 */
	private Date paymentTime;
	/**
	 * 发货时间
	 */
	private Date deliveryTime;
	/**
	 * 确认收货时间
	 */
	private Date receiveTime;
	/**
	 * 评价时间
	 */
	private Date commentTime;
	/**
	 * 修改时间
	 */
	private Date modifyTime;

	@TableField(exist = false)
	private List<OrderItem> orderItems;

	@TableField(exist = false)
	private String statusString;

	public String getStatusString() {
		switch (Objects.requireNonNull(getByCode(status))) {
			case CREATE_NEW:
				statusString = CREATE_NEW.getMsg();
				break;
			case PAYED:
				statusString = PAYED.getMsg();
				break;
			case DELIVERED:
				statusString = DELIVERED.getMsg();
				break;
			case FINISHED:
				statusString = FINISHED.getMsg();
				break;
			case CLOSED:
				statusString = CLOSED.getMsg();
				break;
			case SERVICING:
				statusString = SERVICING.getMsg();
				break;
			case SERVICED:
				statusString = SERVICED.getMsg();
				break;
			default:  statusString = "未知状态";
		}
		return statusString;
	}

	public static Order fromTo(OrderTo to) {
		return BeanUtil.copyProperties(to, Order.class);
	}

	public OrderTo toTo() {
		return BeanUtil.copyProperties(this, OrderTo.class);
	}

	/**
	 * 只有待付款状态的订单才能关闭
	 */
	public boolean closeable() {
		return this.status.equals(CREATE_NEW.getCode());
	}

	/**
	 * 订单状态修改为 “已关闭”
     * @param id 订单ID
     */
	public void close(Long id) {
		this.setId(id);
		this.setStatus(OrderConstant.StatusEnum.CLOSED.getCode());
	}
}
